# include <iostream>
# include <string>
# include <fstream>
using namespace std;

void main ()

{
	ifstream inputFile;
	cout<<"Please Enter File Name:";
	string Fname;
	cin>>Fname;
	inputFile.open(Fname.c_str());
	int line=1;
	while(!(inputFile.eof())&& !inputFile.fail())
	{
		bool Error=false;
		string instOperator;
		inputFile>>instOperator;//Get Instruction Operator
		for(int i = 0; i< instOperator.length(); i++)//Convert to lower case
			instOperator[i] = tolower(instOperator[ i ]);

		//Check for R-Type
		if(instOperator=="add"||instOperator=="sub"||instOperator=="and"||instOperator=="slt")
		{
			int register1,register2,register3;
			inputFile>>register1;// Get first register
			if(!(register1>=0 && register1<=15))
				Error=true;
			inputFile>>register2;// Get second register
			if(!(register2>=0 && register2<=15))
				Error=true;
			inputFile>>register3;// Get third register
			if(!(register3>=0 && register3<=15))
				Error=true;
			string temp;
			getline(inputFile,temp);
			if(temp!="")
				Error=true;
		}

		//Check I-Type
		else if(instOperator=="adi"||instOperator=="beq"||instOperator=="bne")
		{
			int register1,register2,register3;
			inputFile>>register1;// Get first register
			if(!(register1>=0 && register1<=15))
				Error=true;
			inputFile>>register2;// Get second register
			if(!(register2>=0 && register2<=15))
				Error=true;
			inputFile>>register3;// Get instruction
			if(!(register3>=-32768 && register3<=32767))// 2^16-1= 65535
				Error=true;
			string temp;
			getline(inputFile,temp);
			if(temp!="")
				Error=true;
		}

		//Check J-Type
		else if(instOperator=="j")
		{
			int register1;
			inputFile>>register1; // Get instruction
			if(!(register1>=0 && register1<=65535))// 2^16-1= 65535
				Error=true;
			string temp;
			getline(inputFile,temp);
			if(temp!="")
				Error=true;
		}

		//Return Error if all is false
		else 
		{
			string temp;
			cout<<"Syntax Error in Line "<<line<<endl;
			getline(inputFile,temp);
		}
		if(Error)
			cout<<"Syntax Error in Line "<<line<<endl;
		line++;
	}
}